package com.ls.task.biz.impl;

import java.util.List;

import org.quartz.JobDetail;
import org.quartz.SchedulerException;
import org.quartz.TriggerKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Service;

import com.ls.fw.commons.log.exception.ExceptionsHelper;
import com.ls.task.biz.SysTaskTriggerBiz;
import com.ls.task.dal.model.SysTask;
import com.ls.task.dal.model.SysTaskTrigger;
import com.ls.task.dal.service.SysTaskFireService;
import com.ls.task.dal.service.SysTaskService;
import com.ls.task.dal.service.SysTaskTriggerService;
import com.ls.task.utils.TaskUtils;

@Service("sysTaskTriggerBiz")
public class SysTaskTriggerBizImpl implements SysTaskTriggerBiz {

	@Autowired
	private SysTaskTriggerService sysTaskTriggerService;
	@Autowired
	private SysTaskService sysTaskService;
	@Autowired
	private SchedulerFactoryBean  quertzManager;

	@Override
	public void save(SysTaskTrigger model){
		this.sysTaskTriggerService.persist(model);
		model = sysTaskTriggerService.find(model.getId());
		SysTask sysTask = sysTaskService.find(model.getTaskId());
		load(model, sysTask);
	}
	
	@Override
	public void load(SysTaskTrigger model, SysTask sysTask) {
		if(model.getValidClass()==Boolean.FALSE
		   || model.getDeleteClass()==Boolean.TRUE
		   || sysTask.getValidClass()==Boolean.FALSE
		   || sysTask.getDeleteClass()==Boolean.TRUE
		   ){
			try {
				TriggerKey triggerKey = TaskUtils.triggerKey(model, sysTask);
				if(quertzManager.getScheduler().checkExists(triggerKey)){
					quertzManager.getScheduler().unscheduleJob(triggerKey);
				}
			} catch (SchedulerException e) {
				throw new IllegalArgumentException(ExceptionsHelper.getRootCause(e).getMessage(),e);
			}
		}else{
			try {
				TriggerKey triggerKey = TaskUtils.triggerKey(model, sysTask);
				if(!quertzManager.getScheduler().checkExists(triggerKey)){
					JobDetail jobDetail = TaskUtils.loadTask(sysTask);
					quertzManager.getScheduler().addJob(jobDetail, true);
				}
			} catch (SchedulerException e) {
				throw new IllegalArgumentException(ExceptionsHelper.getRootCause(e).getMessage(),e);
			}
			try {
				TriggerKey triggerKey = TaskUtils.triggerKey(model, sysTask);
				if(!quertzManager.getScheduler().checkExists(triggerKey)){
					quertzManager.getScheduler().scheduleJob(TaskUtils.trigger(null, model, sysTask));
				}
			} catch (SchedulerException e) {
				throw new IllegalArgumentException(ExceptionsHelper.getRootCause(e).getMessage(),e);
			}
		}
	} 
	private void unscheduleJob(Long id) {
		SysTaskTrigger model = sysTaskTriggerService.find(id);
		SysTask sysTask = sysTaskService.find(model.getTaskId());
		try {
			TriggerKey triggerKey = TaskUtils.triggerKey(model, sysTask);
			if(quertzManager.getScheduler().checkExists(triggerKey)){
				quertzManager.getScheduler().unscheduleJob(triggerKey);
			}
		} catch (SchedulerException e) {
			throw new IllegalArgumentException(ExceptionsHelper.getRootCause(e).getMessage(),e);
		}
	}
	@Override
	public void persist(SysTaskTrigger forModel) {
		save(forModel);
	}

	@Override
	public void update(SysTaskTrigger forModel) {
		save(forModel);
	}

	@Override
	public void remove(String[] ids) {
		if(ids !=null){
			for (String id : ids) {
				int i = sysTaskTriggerService.remove(new String[]{id+""});
				if(i>0){
					unscheduleJob(Long.parseLong(id));
				}
			}
		}
	}

	@Override
	public void delete(String[] ids) {
		if(ids !=null){
			try {
				for (String idstr : ids) {
					long id = Long.parseLong(idstr);
					SysTaskTrigger model = sysTaskTriggerService.find(id);
					SysTask sysTask = sysTaskService.find(model.getTaskId());
					int i = sysTaskTriggerService.delete(id);
					if(i > 0){
						TriggerKey triggerKey = TaskUtils.triggerKey(model, sysTask);
						if(quertzManager.getScheduler().checkExists(triggerKey)){
							quertzManager.getScheduler().unscheduleJob(triggerKey);
						}
					}
				}
			} catch (SchedulerException e) {
				throw new IllegalArgumentException(ExceptionsHelper.getRootCause(e).getMessage(),e);
			}
		}
	}

	@Override
	public void load(){
		//DOTO 特别情况：注释掉下面的语句导致jdbc链接卡住
		System.out.println("sysTaskTriggerService："+sysTaskTriggerService);
		SysTaskTrigger record = new SysTaskTrigger();
		record.setDeleteClass(Boolean.FALSE);
		record.setValidClass(Boolean.TRUE);
		List<SysTaskTrigger> list = sysTaskTriggerService.select(record );
		if(list != null && list.size()>0){
			for (SysTaskTrigger sysTaskTrigger : list) {
				load(sysTaskTrigger,this.sysTaskService.find(sysTaskTrigger.getTaskId()));
			}
		}
	}
	 
}
